package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.R;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.*;
import jdk.nashorn.internal.objects.annotations.Where;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;;
import java.util.stream.Collectors;

/**
 * @author wuyiqiang
 * @2022/6/14 16:03
 */
@Service
public class DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 通过id查菜品中的分类id
     *
     * @param id
     * @return
     */
    public int selectByCid(String id) {
        return dishMapper.selectByCid(id);
    }

    /**
     * 新增菜品
     *
     * @param dishDto
     */
    public void save(DishDto dishDto) {
        String dishId = System.currentTimeMillis() + "";
        dishDto.setId(dishId);
        //获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        dishDto.setCreateTime(date);
        dishDto.setUpdateTime(date);
        //状态
        dishDto.setStatus(1);
        //默认排序
        dishDto.setSort(0);
        //添加信息到菜品中
        dishMapper.save(dishDto);
        // 取出口味的信息，并且给每一个口味对象补全信息
        List<DishFlavor> flavorList = dishDto.getFlavors();

        //回顾stream流  map就是底层hi帮你遍历元素，遍历出来的每一个元素都交给你，你进行加工处理。 返回出去的元素会交给收集器collect,  返回一个集合
        //这个集合存储了加工后的元素
        flavorList = flavorList.stream().map((dishFlavor -> {
            dishFlavor.setId(dishId);
            dishFlavor.setDishId(dishId);
            dishFlavor.setCreateTime(date);
            dishFlavor.setUpdateTime(date);
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            return dishFlavor;
        })).collect(Collectors.toList());
        //把口味数据返回给数据库
        dishFlavorMapper.batchsave(flavorList);

    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    public R<Page> findByPage(Integer page, Integer pageSize, String name) {
        page = (page - 1) * pageSize;
        //根据名字查询菜品列表
        List<Dish> dishList = dishMapper.findByName(name, page, pageSize);

        Page page2 = new Page();
        //把dish转化为dishDTO
        List<DishDto> dishDtoList = null;
        try {
            dishDtoList = dishList.stream().map(dish -> {
                DishDto dishDto = new DishDto();
                //属性拷贝，属性拷贝的前提：属性名和属性类型一致
                BeanUtils.copyProperties(dish, dishDto);
                //根据类别的id查找类别
                Category category = categoryMapper.selectById(dishDto.getCategoryId());
                System.out.println(dishDto.getId() + "-========================菜品分页查询");
                dishDto.setCategoryName(category.getName());
                return dishDto;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            System.out.println("菜品分页查询=================debug");
        }

        //设置返回页的总条数
        page2.setTotal(dishMapper.selectAll());
        //设置返回页的每页数量
        page2.setSize(pageSize);
        if (dishDtoList != null) {
            //设置返回页返回类对象的当前页
            page2.setCurrent(page);
            //将获得到的数据写进返回类中
            page2.setRecords(dishDtoList);
            return R.success(page2);
        } else {
            return R.error("没有数据了");
        }

    }

    /**
     * 根据id查询菜品信息和口味
     *
     * @param id
     * @return
     */
    public DishDto selectById(String id) {
        //查询菜品的基本信息
        Dish dish = dishMapper.selectById(id);


        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味信息
        List<DishFlavor> dishFlavor = dishFlavorMapper.selectByDishId(dish.getId());
        dishDto.setFlavors(dishFlavor);
        return dishDto;


    }


    /**
     * 修改状态
     *
     * @param status
     * @param ids
     */
    public void status(Integer status, List<String> ids) {
        for (String id : ids) {
            dishMapper.updateStatus(id, status);
        }
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     */
    public void update(DishDto dishDto) {
        //修改时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        dishDto.setUpdateTime(date);
        System.out.println(dishDto.getCategoryId() + "==================service1");
        //修改菜品
        dishMapper.updateById(dishDto);
        //3. 删除该菜品的所有口味信息
        dishFlavorMapper.deleteByDishId(dishDto.getId());
        //4. 补全口味信息，然后重新插入
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> dishFlavorList = flavors.stream().map((flavor -> {
            //对每一个元素进行加工
            flavor.setId(dishDto.getId());
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(date);
            flavor.setUpdateTime(date);
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());

        //4. 批量保存口味信息
        dishFlavorMapper.batchsave(dishFlavorList);

        System.out.println(dishDto.getCategoryId() + "====================service2");
    }

    /**
     * 删除
     * @param ids
     */
    public void delete(List<String> ids){
        for (String id : ids) {
            dishMapper.delete(id);
        }
    }

    /**
     * 下拉查询
     * @param categoryId
     * @return
     */
    public R dropDown(String categoryId){
        List<Dish> dishList = dishMapper.categoryId(categoryId);
        System.out.println(categoryId+"===================下拉查询");
        return R.success(dishList);
    }
}
